package com.micro.school.system.service.impl;

import java.util.List;

import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.utils.DateUtils;
import com.micro.school.common.core.utils.SecurityContextHolderUtils;
import com.micro.school.common.security.utils.SecurityUtils;
import com.micro.school.system.api.domain.vo.StudentPayVos;
import com.micro.school.system.api.model.LoginUser;
import com.micro.school.system.mapper.StudentInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.micro.school.system.mapper.StudentPayMapper;
import com.micro.school.system.domain.StudentPay;
import com.micro.school.system.domain.vo.StudentPayVo;
import com.micro.school.system.convert.student.StudentPayConvert;
import com.ruoyi.system.service.IStudentPayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author rakic
 * @date 2025-08-05
 */
@Service
public class StudentPayServiceImpl extends ServiceImpl<StudentPayMapper,StudentPay> implements IStudentPayService
{
    @Autowired
    private StudentPayMapper studentPayMapper;

    @Autowired
    private StudentInfoMapper studentInfoMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public StudentPayVo selectStudentPayById(Long id)
    {

        return studentPayMapper.selectStudentPayById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param studentPayVo 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<StudentPayVo> selectStudentPayList(StudentPayVo studentPayVo)
    {
        return studentPayMapper.selectStudentPayList(studentPayVo);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param studentPayVo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertStudentPay(StudentPayVo studentPayVo)
    {
        StudentPay studentPay = StudentPayConvert.INSTANCE.convert(studentPayVo);

        studentPay.setCreateTime(DateUtils.getNowDate());

        return studentPayMapper.insert(studentPay);

    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param studentPayVo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateStudentPay(StudentPayVo studentPayVo)
    {
        StudentPay studentPay = StudentPayConvert.INSTANCE.convert(studentPayVo);
        studentPay.setUpdateTime(DateUtils.getNowDate());
        return studentPayMapper.updateStudentPay(studentPay);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteStudentPayByIds(List<Long> ids)
    {

        return studentPayMapper.deleteStudentPayByIds(ids, Integer.parseInt(SecurityContextHolder.getUserTenantId()));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteStudentPayById(Long id)
    {

        return studentPayMapper.deleteStudentPayById(id, Integer.parseInt(SecurityContextHolder.getUserTenantId()));
    }

    @Override
    public int insertStudentPayList(List<StudentPayVos> studentPayVosList) {
        //学生和学年都是统一的
        List<StudentPayVos> studentPayVosList1 = studentPayMapper.selectStudentPay(studentPayVosList.get(0).getStudentId(),studentPayVosList.get(0).getFeeYearId());
        if (studentPayVosList1 != null && studentPayVosList1.size() > 0){
            //写入过就不再写入
            return 0;
        }else{
            //支付成功后需要修改学生状态
            studentInfoMapper.updatePayStatus(studentPayVosList.get(0).getStudentId());
            return studentPayMapper.insertBatch(studentPayVosList);
        }

    }

    @Override
    public int updateStudentPayList(List<StudentPayVos> studentPayVosList) {
        List<StudentPayVos> studentPayVosList1 = studentPayMapper.selectStudentPay(studentPayVosList.get(0).getStudentId(),studentPayVosList.get(0).getFeeYearId());
        if (studentPayVosList1 != null && studentPayVosList1.size() > 0){
            //写入过就不再写入
            return 0;
        }else{
            return studentPayMapper.updateStudentPayList(studentPayVosList);
        }
    }

    @Override
    public List<StudentPay> getAllLog(Long studentId) {
        List<StudentPay> studentPayList = studentPayMapper.selectGetAllLog(studentId);
        return studentPayList;
    }

}
